package com.sunmouse.toolkit.fragment.chart;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sunmouse.toolkit.R;
import com.sunmouse.toolkit.constant.Material;
import com.sunmouse.toolkit.fragment.BaseFragment;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import lecho.lib.hellocharts.model.PieChartData;
import lecho.lib.hellocharts.model.SliceValue;
import lecho.lib.hellocharts.view.PieChartView;

/**
 * A simple {@link Fragment} subclass.
 */
public class PieFragment extends BaseFragment {


    @BindView(R.id.pie_chart)
    PieChartView pieChart;
    Unbinder unbinder;

    public PieFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_pie, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void init() {
        ArrayList<SliceValue> values = generateSlices();
        PieChartData pieData = generatePieData(values);
        this.pieChart.setPieChartData(pieData);
    }

    private PieChartData generatePieData(ArrayList<SliceValue> values) {
        PieChartData data = new PieChartData(values);
        data.setAxisXBottom(generateXAxis());
        data.setAxisYLeft(generateYAxis());
        data.setHasLabels(true);
//        mData.setHasLabelsOutside(true);
        data.setHasCenterCircle(true);
        data.setCenterText1("Battery");//should setHarCenterCircle(true)
        data.setCenterText1Color(Material.COLORS[0]);
        return data;
    }

    private ArrayList<SliceValue> generateSlices() {
        ArrayList<SliceValue> slices = new ArrayList<>();
        for (int i = 0; i < 7; i++) {
            SliceValue va = new SliceValue((float) (30 * Math.random() + 15.0f), getCircleColor());
            va.setLabel((int) va.getValue() + "%");
            slices.add(va);
        }
        return slices;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
